module hdmi_ip_top (
    input           resetn,
    // RGB显示接口时钟
    input           rgb_pclk,
    input   [23:0]  rgb_data,
    input           rgb_hsync,
    input           rgb_vsync,
    input           rgb_de,
    // HDMI转发时钟（5倍PCLK时钟）
    input           serclk,
    // HDMI TMDS差分输出
    output          hdmi_clk_p,
    output          hdmi_clk_n,
    output  [2:0]   hdmi_data_p,
    output  [2:0]   hdmi_data_n
);

wire  [7:0]   rgb_r;
wire  [7:0]   rgb_g;
wire  [7:0]   rgb_b;
assign rgb_data = {rgb_r,rgb_g,rgb_b};
wire [9:0] encoded_r;
wire [9:0] encoded_g;
wire [9:0] encoded_b;

// 8b/10b编码器（只有B通道编码了hsync和vsync信号）
encoder_8bto10b encoder_r(
    .clk    (rgb_pclk),
    .resetn (resetn),
    .din    (rgb_r),
    .c0     (1'b0),
    .c1     (1'b0),
    .de     (rgb_de),
    .dout   (encoded_r)
);

encoder_8bto10b encoder_g(
    .clk    (rgb_pclk),
    .resetn (resetn),
    .din    (rgb_g),
    .c0     (1'b0),
    .c1     (1'b0),
    .de     (rgb_de),
    .dout   (encoded_g)
);

encoder_8bto10b encoder_b(
    .clk    (rgb_pclk),
    .resetn (resetn),
    .din    (rgb_b),
    .c0     (rgb_hsync),
    .c1     (rgb_vsync),
    .de     (rgb_de),
    .dout   (encoded_b)
);

// 并转串模块
wire serialized_r,serialized_g,serialized_b,serialized_clk;

serializer_10b serializer_10b_clk(
    .resetn (resetn),
    .dclk   (dclk),
    .serclk (serclk),
    .din    (10'b1111100000),
    .dout   (serialized_clk)
);

serializer_10b serializer_10b_r(
    .resetn (resetn),
    .dclk   (dclk),
    .serclk (serclk),
    .din    (encoded_r),
    .dout   (serialized_r)
);

serializer_10b serializer_10b_g(
    .resetn (resetn),
    .dclk   (dclk),
    .serclk (serclk),
    .din    (encoded_g),
    .dout   (serialized_g)
);

serializer_10b serializer_10b_b(
    .resetn (resetn),
    .dclk   (dclk),
    .serclk (serclk),
    .din    (encoded_b),
    .dout   (serialized_b)
);

ser2tmds ser2tmds_inst(
    .ser_clk_i  (serialized_clk),
    .ser_data_i ({serialized_r,serialized_g,serialized_b}),

    .tmds_clk_p (hdmi_clk_p),
    .tmds_clk_n (hdmi_clk_n),

    .tmds_data_p    (hdmi_data_p),
    .tmds_data_n    (hdmi_data_n)
);
endmodule